//
//  SPPettingAddController.swift
//  SparklingPets
//
//  Created by iOS-Czz on 2023/10/26.
//

import UIKit
import WMZDialog
import HXPhotoPicker_Lite
class SPPettingAddController: SPBaseController{
    lazy var typeView: SPPetTypeView = {
        let view = Bundle.main.loadNibNamed("SPPetTypeView", owner: self, options: nil)?.first as! SPPetTypeView
        view.frame = keyWindow!.frame
        view.selectIndex = 6
        return view
    }()
    @IBOutlet var size1NameTF: UITextField!
    @IBOutlet var size1TF: UITextField!
    
    @IBOutlet var size2NameTF: UITextField!
    @IBOutlet var size2TF: UITextField!
    
    @IBOutlet var size3NameTF: UITextField!
    @IBOutlet var size3TF: UITextField!
    
    @IBOutlet var nameTF: UITextField!
    @IBOutlet var petTypeTF: UITextField!
    @IBOutlet var picture: UIImageView!
    @IBOutlet var petIcon: UIImageView!
    @IBOutlet var petNameLabel: UILabel!
    var petType = 6
    var selectImage:UIImage? = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavStyle()
        // Do any additional setup after loading the view.
    }
    func setNavStyle(){
        self.title = "Log addition"
        self.addRightBarButton(imageName: "icon_save")
        
    }
    @IBAction func clickCloseAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wx_navigationBar.isHidden = true
    }
    @IBAction func clockSaveAction() {
        self.view.endEditing(true)
        if self.nameTF.text!.isEmpty {
            UIView.showTipsView("Please enter pet name")
            return
        }
        if self.petNameLabel.text!.isEmpty {
            UIView.showTipsView("Please select pet type")
            return
        } 
        if self.petType == 6 {
            UIView.showTipsView("Please select pet type")
            return
        }
        if self.size1NameTF.text!.isEmpty {
            UIView.showTipsView("Please enter label 1")
            return
        }
        if self.size1TF.text!.isEmpty {
            UIView.showTipsView("Please enter content")
            return
        }
        if self.size2NameTF.text!.isEmpty {
            UIView.showTipsView("Please enter label 2")
            return
        }
        if self.size2TF.text!.isEmpty {
            UIView.showTipsView("Please enter content")
            return
        }
        if self.size3NameTF.text!.isEmpty {
            UIView.showTipsView("Please enter label 3")
            return
        }
        if self.size3TF.text!.isEmpty {
            UIView.showTipsView("Please enter content")
            return
        }
        if selectImage == nil {
            UIView.showTipsView("Please select a pet picture")
            return
        }
        let model = SPPetModel()
        model.picture = UIImage.saveImageInCache(self.selectImage!)!
        model.type = self.petType
        model.name = self.nameTF.text!
        model.size1 = self.size1TF.text!
        model.sizeName1 = self.size1NameTF.text!
        model.size2 = self.size2TF.text!
        model.sizeName2 = self.size2NameTF.text!
        model.size3 = self.size3TF.text!
        model.sizeName3 = self.size3NameTF.text!
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView("Add pet succeed", true)
        let helper = SPPetModel.getUsingLKDBHelper()
        helper.delete(with: SPPetModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    @IBAction func tapChoosePetTypeAction(_ sender: Any) {
        self.typeView.onSave = { index in
            self.petTypeTF.isHidden = true
            self.petIcon.isHidden = false
            self.petNameLabel.isHidden = false
            self.petIcon.image = UIImage(named: PET_ICONS[index])
            self.petNameLabel.text = PET_NAMES[index]
            self.petType = index
        }
        keyWindow?.addSubview(self.typeView)
    }
    
    
    @IBAction func tapAddPictureAction(_ sender: Any) {
        self.view.endEditing(true)
        var config = PickerConfiguration.default
        config.languageType = .english
        config.maximumSelectedCount = 1
        config.photoSelectionTapAction = .quickSelect
        config.modalPresentationStyle = .fullScreen
        config.selectMode = .single
        config.photoList.allowAddCamera = false
        Photo.picker(
            config
        ) { [self] result, pickerController in
            
            result.getImage(completionHandler: { [self] images in
                picture.image = images.first
                selectImage = images.first
            })
        } cancel: { pickerController in
            // 取消的回调
        }
    }
    
    
    
}
